package com.grad.webstudy.utils;

import com.grad.webstudy.constants.RedisConstants;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: 日期工具类
 * @Author: zjf
 * @Date: 2020/1/27 17:18.
 */
public class DateUtils {

    public static String toString(Date date, String pattern) {
        String stringTime = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern(pattern);
        stringTime = simpleDateFormat.format(date);
        return stringTime;
    }


    public static List<String> getPastDate(String format,Integer days) {
        Calendar calendar = Calendar.getInstance();

        List<String> dateList = new ArrayList<>();

        dateList.add(DateUtils.toString(new Date(), format));

        for (int i = 1; i < days; i++) {
            dateList.add(getPastDate(i,format));
        }
        return dateList;
    }

    public static String getPastDate(int past,String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String result = format.format(today);
        return result;
    }
}
